﻿/***************************************************************************
 //* Foxit Software Inc.
 //* Copyright(C) 2005-2014, all rights reserved.
 //*
 //* The following code is copyrighted and contains proprietary information
 //* and trade secrets of Foxit Software Inc.                                                      *
 ***************************************************************************/

/**
 * 基础按钮类，用来响应对应的command消息;
 */
define(function (require, exports, module) {
    var WebPDF = require('./WebPDF');
    WebPDF.CCommandUI = function (readerApp, uiDefinition) {
        this.name = uiDefinition.name;
        this.commandName = uiDefinition.commandName;
        this.createHtmlFunc = uiDefinition.createHtml;
        this.onCommandStateChange = uiDefinition.onCommandStateChange;
        this.initFunc = uiDefinition.init;
        this.state = 0;
        if (uiDefinition.state) {
            this.state = uiDefinition.state;
        }
        var _self = this;

        /**
         * Create html for UI item.
         * @returns {String} Html for UI item.
         */
        this.createHtml = function () {
            return _self.createHtmlFunc.call(this, readerApp);
        };
        /**
         * Initialize UI item.
         */
        this.init = function (readerApp) {
            if (_self.onCommandStateChange != null) {
                var command = readerApp.getCommand(_self.commandName);
                if (command) {
                    $(command).on("StateChange." + readerApp.getAppId(),function (event,data) {
                            if (_self.state === data)
                                return;
                            _self.onCommandStateChange.call(this, data);
                            _self.state = data;
                    });
                }
            }
            if (_self.initFunc != null) {
                _self.initFunc.call(this);
            }
        };
    };
    return WebPDF.CCommandUI;
});